# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.db import models
from apps.news.models import Record

class Comment(models.Model):

    STATUS_MODERATION = 0
    STATUS_PUBLISHED = 1
    STATUS_BLOCKED = 2
    STATUS_DELETED = 3

    STATUSES = (
        (STATUS_MODERATION, "На модерации"),
        (STATUS_PUBLISHED, "Опубликован"),
        (STATUS_BLOCKED, "Заблокирован"),
        (STATUS_DELETED, "Удален")
    )

    SMILES = {
        'acute': "/static/images/smiles/acute.gif",
        'bad': "/static/images/smiles/bad.gif",
        'biggrin': "/static/images/smiles/biggrin.gif",
        'blum': "/static/images/smiles/blum.gif",
        'blush': "/static/images/smiles/blush.gif",
        'bomb': "/static/images/smiles/bomb.gif",
        'bye': "/static/images/smiles/bye.gif",
        'clapping': "/static/images/smiles/clapping.gif",
        'cool': "/static/images/smiles/cool.gif",
        'diablo': "/static/images/smiles/diablo.gif",
        'fool': "/static/images/smiles/fool.gif",
        'good': "/static/images/smiles/good.gif",
        'greeting': "/static/images/smiles/greeting.gif",
        'mad': "/static/images/smiles/mad.gif",
        'morning': "/static/images/smiles/morning.gif",
        'music': "/static/images/smiles/music.gif",
        'negative': "/static/images/smiles/negative.gif"
    }

    user = models.ForeignKey(User)
    message = models.TextField(max_length=512)
    date = models.DateTimeField(auto_now_add=True)
    rating = models.IntegerField(default=0)
    status = models.IntegerField(choices=STATUSES, default=STATUS_MODERATION)

    uid = models.IntegerField(db_index=True)

    parent = models.ForeignKey('self', null=True, blank=True)

    def childs(self):
        return self.comment_set.filter(status = self.STATUS_PUBLISHED)

    @staticmethod
    def calculate_url(url):
        unique_id = 0
        for symbol in url:
            unique_id += ord(symbol)
        return unique_id

    def __str__(self):
        return self.message

    def __unicode__(self):
        return self.message

    class Meta:
        db_table = "comments"


class CommentVote(models.Model):
    user = models.ForeignKey(User)
    comment = models.ForeignKey(Comment)


class CommentNotification(models.Model):
    user = models.ForeignKey(User)
    record = models.ForeignKey(Record)
    comments = models.IntegerField(default=0)

    class Meta:
        db_table = "comments_notifications"
